package org.dendy.app.ssh;

import java.io.ByteArrayInputStream;
import java.util.Properties;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class Executed {

	static JSch jsch;

	static String user;

	static String pass;

	static String host;

	static Session session;

	static Properties properties;

	static ChannelExec channelExec;

	static Channel channel;

	static Display display;

	static Shell shell;

	static CLabel label;

	/**
	 * methon untuk button reboot
	 * 
	 * @throws JSchException
	 */
	static void buttonRebot() throws JSchException {
		channel = session.openChannel("shell");
		channel.setInputStream(System.in);
		ByteArrayInputStream bi = new ByteArrayInputStream(
				"init 6\r".getBytes());
		channel.setInputStream(bi);
		channel.connect(3000);
	}

	/**
	 * Method untuk button off
	 * 
	 * @throws JSchException
	 */
	static void buttonOff() throws JSchException {
		channel = session.openChannel("shell");
		channel.setInputStream(System.in);
		ByteArrayInputStream bi = new ByteArrayInputStream(
				"init 0\r".getBytes());
		channel.setInputStream(bi);
		channel.connect(3000);
	}

	public static void main(String[] args) throws JSchException {

		jsch = new JSch();

		user = "root";
		pass = "andika";
		host = "192.168.0.101";

		session = jsch.getSession(user, host, 222);
		session.setPassword(pass);
		properties = new Properties();
		properties.put("StrictHostKeyChecking", "no");
		session.setConfig(properties);
		session.connect();
		System.out.println("connect ke server");
		/*
		 * perintah eksekusi matiin server
		 */
		String command = "init 0";

		channelExec = (ChannelExec) session.openChannel("exec");
		channelExec.setCommand(command);
		channelExec.setInputStream(System.in);
		((ChannelExec) channelExec).setErrStream(System.err);

		display = new Display();
		shell = new Shell(display);
		shell.setLayout(new FillLayout());
		shell.setLayout(new GridLayout(1, false));
		shell.setText("Please Enter ");

		label = new CLabel(shell, SWT.CENTER);
		label.setText("IP Address : 192.168.0.101");

		Button powerOff = new Button(shell, SWT.PUSH | SWT.CENTER);
		powerOff.setText("Init 0");
		powerOff.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				try {
					buttonOff();
				} catch (JSchException e1) {
					System.err.println("Not Connection");
				}
			}
		});

		Button reboot = new Button(shell, SWT.PUSH);
		reboot.setText("init 6");
		reboot.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				try {
					buttonRebot();
				} catch (JSchException e1) {
					System.err.println("Not Connections");
				}
			}
		});

		shell.setSize(350, 250);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();

	}
}
